package com.nb.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nb.domain.Employee;
import com.nb.result.Result;
import com.nb.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest request,@RequestBody Employee employee){

        //1.md5加密password
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.验证数据库是否有登录的用户名
        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
        //校验过程不要在内存中(Java代码内)进行，送到数据库进行校验操作
        lqw.eq(Employee :: getUsername,username);
        lqw.eq(Employee :: getPassword,password);
        //将查询的结果封装
        Employee one = employeeService.getOne(lqw);
        //3.验证失败，返回错误信息
        if (one == null) {
            return Result.error("用户名不存在！");
        }
        //4.用户名验证成功，继续验证密码，失败则返回信息
        if (!password.equals(one.getPassword())) {
            return Result.error("密码错误！");
        }
        //5.密码验证成功，验证账号状态，若被禁用则返回信息
        if (one.getStatus() == 0) {
            return Result.error("该帐号已被禁用！");
        }
        //6.账号状态验证成功，将员工id存入session，向前端返回查找的对象信息
        request.getSession().setAttribute("employee",one.getId());
        //返回前将密码清空，避免返回至浏览器端泄露
        one.setPassword(null);
        return Result.success(one);
    }

    @PostMapping("/logout")
    public  Result<String> logout(HttpServletRequest request){
//        移除当前session中保存的id
        request.getSession().removeAttribute("employee");
//        当移除的数据量较多时，选择直接销毁session对象
//        request.getSession().invalidate();
        return Result.success("退出成功！");
    }

    @PostMapping
    public Result<String> add(HttpServletRequest request,@RequestBody Employee employee){
        //md5 password
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        //获取新增员工的时间
//        employee.setCreateTime(LocalDateTime.now());
//        //获取更新信息的时间
//        employee.setUpdateTime(LocalDateTime.now());
        //获得当前管理员的id
//        Long id = (Long)request.getSession().getAttribute("employee");
//        //记录新增员工的管理员id
//        employee.setCreateUser(id);
//        //记录更新员工信息的管理员id
//        employee.setUpdateUser(id);
        //调用service
        employeeService.save(employee);
        return Result.success("添加成功！");
    }

    @GetMapping("/page")
    //
    public Result<Page> page(int page,int pageSize,String name){
        //分页构造器
        Page pageInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> lqw= new LambdaQueryWrapper<>();
        //过滤条件 当name不为空时
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //排序条件
        lqw.orderByDesc(Employee::getUpdateTime);
        //查询
        employeeService.page(pageInfo,lqw);
        return Result.success(pageInfo);
    }

    @PutMapping
    public Result<String> update(HttpServletRequest request,@RequestBody Employee employee){
        //记录更新的时间
        employee.setUpdateTime(LocalDateTime.now());
        //记录更新人--》管理员id
        Long id = (Long)request.getSession().getAttribute("employee");
        employee.setUpdateUser(id);
        //更新
        employeeService.updateById(employee);
        return Result.success("修改成功！");
    }

    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable("id") Long id){
        Employee employee = employeeService.getById(id);
        if (employee == null) {
            return Result.error("该员工不存在！");
        }
        return Result.success(employee);
    }



}
